package com.lei.thread;

import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author LeiXiaoLei
 * @Date 2022/9/4 10:26
 * @Version 1.0
 */
public class Main {
    public static void main(String[] args) {
        Integer[] arr = {1,2};
        int a = arr.length;

        AtomicInteger res = new AtomicInteger(0);
        Lock lock = new ReentrantLock();
        Semaphore semaphore = new Semaphore(1);
        Thread t1 = new Thread(() -> {
            try {
                semaphore.acquire();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            for (int i = 1; i < 50; i++) {
                res.addAndGet(i);
                System.out.println(Thread.currentThread() + "=>" + res);
            }
            semaphore.release();
        });
        Thread t2 = new Thread(() -> {
            try {
                semaphore.acquire();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            for (int i = 50; i <= 100; i++) {
                res.addAndGet(i);
                System.out.println(Thread.currentThread() + "=>" + res);
            }
            semaphore.release();
        });

//        Thread t1 = new Thread(() -> {
//            lock.lock();
//            for (int i = 1; i <= 50; i++) {
//                res.addAndGet(i);
//                System.out.println(Thread.currentThread() + "=>" + res);
//            }
//            lock.unlock();
//        });
//        Thread t2 = new Thread(() -> {
//            lock.lock();
//            for (int i = 50; i < 100; i++) {
//                res.addAndGet(i);
//                System.out.println(Thread.currentThread() + "=>" + res);
//            }
//            lock.unlock();
//        });
        t1.start();
        t2.start();
    }
}
